package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Mr.Yang
 * @since 2024-05-31
 */
@Api(tags = "用户2.0接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "查询用户表所有数据")
    @GetMapping
    public Result<List<User>> findAll() {
        return Result.success(userService.list()).msg("查询成功");
    }


    /**
     * 根据用户id查找昵称接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "根据用户id查找昵称接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true)
    })
    @GetMapping("/findNicknameByUserId/{id}")
    public Result<User> findNicknameByUserId(@PathVariable Integer id) {
        return Result.success(userService.findNicknameByUserId(id)).msg("查询成功");
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "根据id查询数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @GetMapping("/{id}")
    public Result<User> findOne(@PathVariable Integer id) {
        return Result.success(userService.getById(id)).msg("查询成功");
    }



    /**
     * 更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "修改个人信息")
    @PutMapping
    public Result<String> save(@RequestBody User user) {
        userService.saveOrUpdate(user);
        return Result.success().msg("更新成功");
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        userService.removeById(id);
        return Result.success().msg("删除成功");
    }
}